package cn.musijas.web;

import cn.musi.order.api.IOrderService;
import cn.musi.product.api.IProductService;
import cn.musijas.result.Result;
import cn.musijas.result.ResultMsg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class BusinessController {
    Logger  logger= LoggerFactory.getLogger(BusinessController.class);
    @Resource
    IOrderService  orderService;
    @Resource
    IProductService  productService;

    @RequestMapping("/0")
    public  Result  createOrder(int  productId) throws Exception {
        int  id=orderService.createWrongOrder(productId);
        return  new Result(ResultMsg.OPERATESUCEESS,id);
    }

    @RequestMapping("/1")
    public Result   optimisticOrder(int  productId) throws Exception {
        int  result=orderService.createOptimisticOrder(productId);
        return  new Result(ResultMsg.OPERATESUCEESS,result);
    }


    @RequestMapping("/2")
    public  Result   createOptimisticOrderUseRedis(int  productId) throws Exception {
        int id= orderService.createOptimisticOrderUseRedis(productId);
        return  new Result(ResultMsg.OPERATESUCEESS,id);
    }

    @RequestMapping("/3")
    public  Result  createOptimisticOrderUseRedisAndKafka(int  productId) throws Exception {
        orderService.createOptimisticOrderUseRedisAndKafka(productId);
        return  new Result(ResultMsg.OPERATESUCEESS);
    }


    @RequestMapping("/getCurrentSales")
    @ResponseBody
    public String getCurrentSale() {
        int currentCount = 0;
        try {
            currentCount = productService.getCurrentCount();
        } catch (Exception e) {
            logger.error("Exception",e);
        }
        logger.info("currentCount={}", currentCount);
        return String.valueOf(currentCount);
    }

}
